<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Location;

/**
 * LocationSearch represents the model behind the search form about `common\models\Location`.
 */
class LocationSearch extends Model
{
    public $id;
    public $category_id;
    public $subcategory_id;
    public $name;
    public $description;
    public $address;
    public $city;
    public $phone;
    public $mobile;
    public $websitelink;
    public $latitude;
    public $longitude;
    public $note;
    public $status_flg;
    public $create_time;
    public $update_time;
    public $del_flg;

    public function rules()
    {
        return [
            [['id', 'category_id', 'subcategory_id', 'status_flg', 'create_time', 'update_time', 'del_flg'], 'integer'],
            [['name', 'description', 'address', 'city', 'phone', 'mobile', 'websitelink', 'note'], 'safe'],
            [['latitude', 'longitude'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'category_id' => Yii::t('app', 'Category ID'),
            'subcategory_id' => Yii::t('app', 'Subcategory ID'),
            'name' => Yii::t('app', 'Name'),
            'description' => Yii::t('app', 'Description'),
            'address' => Yii::t('app', 'Address'),
            'city' => Yii::t('app', 'City'),
            'phone' => Yii::t('app', 'Phone'),
            'mobile' => Yii::t('app', 'Mobile'),
            'websitelink' => Yii::t('app', 'Websitelink'),
            'latitude' => Yii::t('app', 'Latitude'),
            'longitude' => Yii::t('app', 'Longitude'),
            'note' => Yii::t('app', 'Note'),
            'status_flg' => Yii::t('app', 'Status Flg'),
            'create_time' => Yii::t('app', 'Create Time'),
            'update_time' => Yii::t('app', 'Update Time'),
            'del_flg' => Yii::t('app', 'Del Flg'),
        ];
    }

    public function search($params)
    {
        $query = Location::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $this->addCondition($query, 'id');
        $this->addCondition($query, 'category_id');
        $this->addCondition($query, 'subcategory_id');
        $this->addCondition($query, 'name', true);
        $this->addCondition($query, 'description', true);
        $this->addCondition($query, 'address', true);
        $this->addCondition($query, 'city', true);
        $this->addCondition($query, 'phone', true);
        $this->addCondition($query, 'mobile', true);
        $this->addCondition($query, 'websitelink', true);
        $this->addCondition($query, 'latitude');
        $this->addCondition($query, 'longitude');
        $this->addCondition($query, 'note', true);
        $this->addCondition($query, 'status_flg');
        $this->addCondition($query, 'create_time');
        $this->addCondition($query, 'update_time');
        $this->addCondition($query, 'del_flg');
        return $dataProvider;
    }

    protected function addCondition($query, $attribute, $partialMatch = false)
    {
        if (($pos = strrpos($attribute, '.')) !== false) {
            $modelAttribute = substr($attribute, $pos + 1);
        } else {
            $modelAttribute = $attribute;
        }

        $value = $this->$modelAttribute;
        if (trim($value) === '') {
            return;
        }
        if ($partialMatch) {
            $query->andWhere(['like', $attribute, $value]);
        } else {
            $query->andWhere([$attribute => $value]);
        }
    }
}
